class Singleton1 {
    private static volatile Singleton1 instance = null;

    private static Object locker = new Object();

    private Singleton1() {//私有构造方法，防止类外创建对象

    }

    public static Singleton1 getInstance() {
        if (instance == null) {
            synchronized(locker) {//效率问题
                if (instance == null) {//线程安全问题
                    instance = new Singleton1();//可能会指令重排序，将instance设置为volatile;
                }
            }
        }
        return instance;
    }

    public static synchronized Singleton1 getInstance1() {
        if (instance == null) {
            instance = new Singleton1();
        }
        return instance;
    }
}

public class Demo2 {
    public static void main(String[] args) {
        Singleton1 singleton1 = Singleton1.getInstance();
        Singleton1 singleton2 = Singleton1.getInstance();

        System.out.println(singleton1 == singleton2);

//        Singleton1 singleton3 = new Singleton1();
    }
}
